package com.myplans.client.presenter;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineHyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.myplans.client.HistoryTokens;
import com.myplans.client.Presenter;
import com.myplans.client.UserUtil;
import com.myplans.client.event.ViewUserEvent;
import com.myplans.client.service.PlanServiceAsync;
import com.myplans.client.service.UserServiceAsync;
import com.myplans.client.util.CookieManager;
import com.myplans.client.view.BaseView;
import com.myplans.shared.Plan;
import com.myplans.shared.User;


public class ViewPlanPresenter implements Presenter {

	public interface Display {

		public Widget asWidget();
		
		public FlowPanel getPlanPanel();
		
		public Label getName();

		public Label getLocation();

		public Label getDate();

		public Label getDescription();
		
		public Label getUser();
		
		public FlowPanel getParticipantsPanel();
		
		public void setIsAttending(boolean isAttending);
		
		public boolean isAttending();

		public Button getAttendingButton();
		
	}

	private HandlerManager eventBus;
	private Display display;
	private UserServiceAsync userServiceAsync;
	private PlanServiceAsync planService;
	private Plan plan;

	public ViewPlanPresenter(HandlerManager eventBus, Display display) {
		this.eventBus = eventBus;
		this.display = display;
		this.bind();
	}

	@SuppressWarnings("deprecation")
	public void go(HasWidgets container) {
		((BaseView)container).getCentralPanel().clear();
		((BaseView)container).getCentralPanel().add(display.asWidget());
		
		display.getName().setText(plan.getName());
		display.getLocation().setText(plan.getLocation() + " - " + plan.getDate());
		display.getDescription().setText(plan.getDescription());
		
		display.getUser().setText(plan.getUser().getName());
		display.getUser().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new ViewUserEvent(plan.getUser()));
			}
		});
		if (plan.getAttendingUsers() != null && plan.getAttendingUsers().size() > 0) {
			for (final User user : plan.getAttendingUsers()) {
				HorizontalPanel horizontalPanel = new HorizontalPanel();
				if (user.getPicture() != null) {
					Image image = new Image(user.getPicture());
					image.setPixelSize(50, 50);
					horizontalPanel.add(image);
				}

				InlineHyperlink link = new InlineHyperlink(user.getName(), 
						HistoryTokens.VIEW_USER.getId());
				link.setStyleName("participantLink");
				link.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						eventBus.fireEvent(new ViewUserEvent(user));
					}
				});
				horizontalPanel.add(link);
				display.getParticipantsPanel().add(horizontalPanel);
			}
		} else {
			display.getParticipantsPanel().add(new Label("Be the first attending to this plan."));
		}
		
		if (CookieManager.getSessionId() != null && UserUtil.user != null) {
			userServiceAsync.getPlansForUser(UserUtil.user, new AsyncCallback<ArrayList<Plan>>() {
				
				@Override
				public void onSuccess(ArrayList<Plan> result) {
					final ArrayList<Plan> userPlans = result;
					display.setIsAttending(userPlans.contains(plan));
					display.getAttendingButton().addClickHandler(new ClickHandler() {
									
									@Override
									public void onClick(ClickEvent event) {
										if (display.isAttending() == true) {
											userServiceAsync.deletePlanForUser(plan, UserUtil.user
													, new AsyncCallback<User>() {
													
														@Override
														public void onSuccess(User result) {
															display.setIsAttending(false);
														}
													
														@Override
														public void onFailure(Throwable caught) {
															Window.alert("Could not de-attend this plan");
														}
											});
										} else {
											userServiceAsync.createPlanForUser(plan, UserUtil.user
													, new AsyncCallback<User>() {
													
														@Override
														public void onSuccess(User result) {
															display.setIsAttending(true);
														}
													
														@Override
														public void onFailure(Throwable caught) {
															Window.alert("Could not attend this plan");
														}
											});
										}
									}
								});
							}

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}
			});
		}
	}

	private void bind() {
	}

	public void setUsersService(UserServiceAsync userServiceAsync) {
		this.userServiceAsync = userServiceAsync;
	}

	public void setPlanService(PlanServiceAsync planServiceAsync) {
		this.planService = planServiceAsync;
	}

	public void setPlan(Plan plan) {
		this.plan = plan;
	}

}
